/**
 * 
 */
package com.informatica.powercenter.sdk.mapfwk.samples;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.informatica.powercenter.sdk.mapfwk.connection.CodePageConstants;
import com.informatica.powercenter.sdk.mapfwk.connection.ConnectionAttributes;
import com.informatica.powercenter.sdk.mapfwk.connection.ConnectionObject;
import com.informatica.powercenter.sdk.mapfwk.exception.RepoConnectionObjectOperationException;
import com.informatica.powercenter.sdk.mapfwk.repository.PmrepRepositoryConnectionManager;
import com.informatica.powercenter.sdk.mapfwk.repository.RepoConnectionInfo;
import com.informatica.powercenter.sdk.mapfwk.repository.RepoPropsConstants;
import com.informatica.powercenter.sdk.mapfwk.repository.Repository;
import com.informatica.powercenter.sdk.mapfwk.repository.RepositoryConnectionManager;


/**
 * @author sramamoo
 *
 */
public class CreateConnectionSample
{
	protected void setProperties(RepoConnectionInfo repoConnectionInfo) throws IOException {
		Properties properties = new Properties();
    	String filename = "pcconfig.properties";
    	InputStream propStream = getClass().getClassLoader().getResourceAsStream( filename);
    	if ( propStream != null ) {
    		properties.load( propStream );
			repoConnectionInfo.setPcClientInstallPath(properties.getProperty(RepoPropsConstants.PC_CLIENT_INSTALL_PATH));
			repoConnectionInfo.setTargetRepoName(properties.getProperty(RepoPropsConstants.TARGET_REPO_NAME));
			repoConnectionInfo.setRepoServerHost(properties.getProperty(RepoPropsConstants.REPO_SERVER_HOST));
			repoConnectionInfo.setRepoServerPort(properties.getProperty(RepoPropsConstants.REPO_SERVER_PORT));
			repoConnectionInfo.setAdminPassword(properties.getProperty(RepoPropsConstants.ADMIN_PASSWORD));
			repoConnectionInfo.setAdminPassword(properties.getProperty(RepoPropsConstants.ADMIN_USERNAME));
    	}
        else {
            throw new IOException( "pcconfig.properties file not found.");
        }
    }
	public static void main(String[] args) throws IOException
	{
		RepositoryConnectionManager rpMgr = new PmrepRepositoryConnectionManager();
		Repository myRepo = new Repository();
		RepoConnectionInfo repoConnectionInfo = new RepoConnectionInfo();
		CreateConnectionSample conSample = null;
		conSample.setProperties(repoConnectionInfo);		
		myRepo.setRepoConnectionInfo(repoConnectionInfo);
		myRepo.setRepositoryConnectionManager(rpMgr);
		// No need to do this, as the above call sets myRepo as the repository for the rpMgr.
		//rpMgr.setRepository(myRepo);
				
		Properties prop = ConnectionAttributes.getDefaultRelationalProperties(ConnectionAttributes.DB_CONN_TYPE_ORACLE);
		prop.list(System.out);
		prop.setProperty(ConnectionAttributes.CONN_ATTR_CONNECTION_NAME, "myConn1");
		prop.setProperty(ConnectionAttributes.CONN_ATTR_CONNECTION_TYPE, ConnectionAttributes.DB_CONN_TYPE_ORACLE);
		prop.setProperty(ConnectionAttributes.CONN_ATTR_USER_NAME, "vganesan");
		prop.setProperty(ConnectionAttributes.CONN_ATTR_PASSWORD,"vganesan");
		prop.setProperty(ConnectionAttributes.CONN_ATTR_CODE_PAGE,CodePageConstants.JEF);
		prop.setProperty(ConnectionAttributes.CONN_ATTR_CONNECT_STRING,"testdb");
		prop.setProperty(ConnectionAttributes.CONN_ATTR_CONNECT_ENV_SQL,"select * from tab;");
		ConnectionObject connObj = new ConnectionObject("myConn1",ConnectionAttributes.CONN_TYPE_RELATIONAL);
		connObj.setConnectionObjectAttr(prop);
			

		try
		{
			myRepo.createConnection(connObj);
		} catch (RepoConnectionObjectOperationException e)
		{
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
	}
}
